﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using bus=WpfDataBingding.TaskServiceReference;

namespace WpfDataBingding.DevTreeData
{
    public class Device
    {
        private string devID;
        private string devType;
        private string name;

        private string monTimeInterval="";//监控时间间隔：用于显示
        private bool isMonitered=false;//标示本设备是否被监控
        private string monitorStatusLabel = "未监控";//用于界面监控状态显示信息...
        private string monitorStatusButton = "添加监控";//用于界面监控状态显示信息...

        private ObservableCollection<DynamicState> dynamicStateList;
        private ObservableCollection<StaticState> staticStateList;

        private MonitorAgent monAgentBelongTo;//所属监控代理的引用


        public string MonTimeInterval//监控时间间隔
        {
            get { return monTimeInterval; }
            set { monTimeInterval = value; }
        }
        /// <summary>
        /// 设置监控间隔
        /// </summary>
        /// <param name="num">整型时间数</param>
        public void setMonTimeInterval(int num)
        {
            if (num <= 0)
                this.monTimeInterval = "";
            this.monTimeInterval = num.ToString();
        }

        public MonitorAgent MonAgentBelongTo//所属监控代理的引用
        {
            get { return monAgentBelongTo; }
            set { monAgentBelongTo = value; }
        }

        public string MonitorStatusLabel//用于界面监控状态显示信息...
        {
            get { return monitorStatusLabel; }
            set { monitorStatusLabel = value; }
        }

        public string MonitorStatusButton//用于界面监控状态显示信息...
        {
            get { return monitorStatusButton; }
            set { monitorStatusButton = value; }
        }

        public string DevID
        {
            get { return devID; }
            set { devID = value; }
        }
        
        public string DevType
        {
            get { return devType; }
            set { devType = value; }
        }

        public bool IsMonitered
        {
            get { return isMonitered; }
            set { isMonitered = value; }
        }

        public ObservableCollection<DynamicState> DynamicStateList
        {
            get { return dynamicStateList; }
            set { dynamicStateList = value; }
        }

        public ObservableCollection<StaticState> StaticStateList
        {
            get { return staticStateList; }
            set { staticStateList = value; }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        override public string ToString()
        {
            if (this.name == null)
                return "";
            return this.name;
        }


        /// <summary>
        /// 设置设备监控状态为true时，界面显示信息设置
        /// </summary>
        public void setMoniterStatus()
        {
            this.isMonitered = true;
            this.monitorStatusButton = "取消监控";
            this.monitorStatusLabel = "已监控";
        }

        /// <summary>
        /// 设置设备监控状态为false时，界面显示信息设置
        /// </summary>
        public void cancelMoniterStatus()
        {
            this.isMonitered = false;
            this.monitorStatusButton = "添加监控";
            this.monitorStatusLabel = "未监控";
        }

        /// <summary>
        /// 根据设备信息构造契约监控结构(用于删除监控)
        /// </summary>
        /// <param name="devSource">源设备对象</param>
        /// <returns>契约监控任务数组</returns>
        public List<bus.UserMonTaskSub> buildMonStructFromDevice_remove()
        {
            WpfDataBingding.DevTreeData.Device devSource = this;
            int taskNum = devSource.DynamicStateList.Count;
            if (taskNum <= 0)
                return null;
            List<WpfDataBingding.TaskServiceReference.UserMonTaskSub> UserMonTaskSubList = new List<bus.UserMonTaskSub>(taskNum);
                WpfDataBingding.TaskServiceReference.UserMonTaskSub userMonTaskSub_e;

            for (int i = 0; i < taskNum; i++)
            {
                userMonTaskSub_e = new bus.UserMonTaskSub();
                userMonTaskSub_e.AgentName = devSource.MonAgentBelongTo.ID;
                userMonTaskSub_e.DevID = devSource.DevID;
                userMonTaskSub_e.DevType = devSource.DevType;
                userMonTaskSub_e.DomainName = "DataBus1";//----debug
                userMonTaskSub_e.StateName = devSource.DynamicStateList[i].Name;
                userMonTaskSub_e.ViewType = devSource.DynamicStateList[i].ViewType;
                UserMonTaskSubList.Add(userMonTaskSub_e);
            }
            return UserMonTaskSubList;
        }

        /// <summary>
        /// 根据设备信息构造契约监控结构(用于添加监控)
        /// </summary>
        /// <param name="devSource">源设备对象</param>
        /// <returns>契约监控任务数组</returns>
        public List<bus.UserMonTaskSub> buildMonStructFromDevice_add(int timeInterval)
        {
            WpfDataBingding.DevTreeData.Device devSource = this;

            int taskNum = devSource.DynamicStateList.Count;
            if (taskNum <= 0)
                return null;
            List<bus.UserMonTaskSub> UserMonTaskSubArray = new List<bus.UserMonTaskSub>(taskNum);
            bus.UserMonTaskSub userMonTaskSub_e;
            for (int i = 0; i < taskNum; i++)
            {
                userMonTaskSub_e = new bus.UserMonTaskSub();
                userMonTaskSub_e.AgentName = devSource.MonAgentBelongTo.ID;
                userMonTaskSub_e.DevID = devSource.DevID;
                userMonTaskSub_e.DevType = devSource.DevType;
                userMonTaskSub_e.DomainName = "DataBus1";//----debug
                userMonTaskSub_e.StateName = devSource.DynamicStateList[i].Name;
                userMonTaskSub_e.ViewType = devSource.DynamicStateList[i].ViewType;
                userMonTaskSub_e.TimeInterval = timeInterval;

                UserMonTaskSubArray.Add(userMonTaskSub_e);
            }
            return UserMonTaskSubArray;
        }

        /// <summary>
        /// 设备是否被监控,非实时
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="monAgentID"></param>
        /// <param name="devID"></param>
        /// <returns></returns>
        public bool isMoniteredFromDB_offline(string userID)//addDeviceMoniterInfo
        {
            string monAgentID = this.monAgentBelongTo.ID;
            string devID = this.devID;
            //new ClientLib.Common.InitMonDevice(userID);
            ClientLib.MonitorState2.Collection coll = (ClientLib.MonitorState2.Collection) System.Windows.Application.Current.Properties["MonitorCollection"];
            bool isMonitered = coll.IsDeviceMonitor(devID, monAgentID);
            return isMonitered;
        }

        /// <summary>
        /// 设备是否被监控,实时
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="monAgentID"></param>
        /// <param name="devID"></param>
        /// <returns></returns>
        public bool isMoniteredFromDB_online(string userID)//addDeviceMoniterInfo
        {
            new ClientLib.Common.InitMonDevice(userID);
            return isMoniteredFromDB_offline(userID);
        }

        /// <summary>
        /// 刷新并填写设备监控信息
        /// </summary>
        /// <param name="userID"></param>
        public void addDeviceMoniterInfo(string userID)
        {
            bool isMonitered = this.isMoniteredFromDB_offline(userID);
            if (isMonitered)
                this.setMoniterStatus();//设备已被监控，设置界面显示信息
            else
                this.cancelMoniterStatus();//设备未被监控，设置界面显示信息
        }

    }
}
